package com.tjec.project.mdmSynchronous.service.impl;

import com.tjec.common.enums.NumCode;
import com.tjec.project.mdmSynchronous.api.JSONUtils;
import com.tjec.project.mdmSynchronous.api.ThreadPost;
import com.tjec.project.mdmSynchronous.domain.MdmUser;
import com.tjec.project.mdmSynchronous.mapper.BmsProjectLeaderMapper;
import com.tjec.project.mdmSynchronous.service.MdmService;
import com.tjec.project.mdmSynchronous.service.MdmUserService;
import com.tjec.project.system.domain.SysUser;
import com.tjec.project.system.mapper.SysUserMapper;
import com.tjec.project.workflow.mapper.BmsWorkflowMapper;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: VillWang
 * @Date: 2020/5/19 18:01
 */
@Service
@Configuration
public class MdmUserServiceImpl implements MdmUserService {

    @Value("${project.http.user}")
    private  String url;
    @Autowired
    private MdmService mdmService;
    
    @Autowired
    private SysUserMapper sysUserMapper;
    
    @Autowired
    private BmsProjectLeaderMapper bmsProjectLeaderMapper;
    
    @Autowired
    private BmsWorkflowMapper bmsWorkflowMapper;
    
    private static Logger logger = LoggerFactory.getLogger(MdmUserServiceImpl.class);
    /**
     *
     *@describe  查询人员的信息
     *@params [mdmUser]
     *@return java.util.List<com.tjec.project.mdmSynchronous.domain.MdmUser>
     *@author VillWang
     *@date 2020/5/19
     *@other
     *
     */
    @Override
    public List<MdmUser> selectMdmUserByName(MdmUser mdmUser) {
        String userName= mdmUser.getUserName();
        String jobNo=mdmUser.getJobNo();
        HashMap<String, String> map = new HashMap<>();
        if(StringUtils.isNotBlank(userName)){
            map.put("userName",userName);
        }
        if(StringUtils.isNotBlank(jobNo)){
            map.put("jobNo",jobNo);
        }
        //1.获取数据
        Map<String, Object> requestString= ThreadPost.postRequestResult(url,map,null);
        if(requestString==null||requestString.isEmpty()){
        }else {
            List<MdmUser> users = dealWith(requestString);
            return  users;
        }
        return null;
    }



    /**
     *
     *@describe  处理返回的json数据
     *@params []
     *@return java.util.List<MdmUser>
     *@author VillWang
     *@date 2020/5/14
     *@other
     *
     */
    public List<MdmUser> dealWith( Map<String, Object> jsonString){
        List<MdmUser> userList=new ArrayList<MdmUser>();
        ArrayList<MdmUser> list =(ArrayList) jsonString.get("list");

        return list;
    }



	@Override
	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public int updateUserJobNo(MdmUser mdmUser) {
		// 1、先通过globalId查到sys_user表中员工编号
		// 2、更新流程表、项目负责人表
		//	  根据原员工编号查找到流程表、项目负责人表的数据，并更新为新的员工编号
		// 3、更新sys_user表中job_no和nick_name
		if(mdmUser!=null && mdmUser.getGlobalId()!=null && !"".equals(mdmUser.getGlobalId())) {
			SysUser user = new SysUser();
			user.setGlobalId(mdmUser.getGlobalId());
			SysUser resUser = sysUserMapper.selectUserByUser(user);
			if(resUser==null){
				return 0;
			}else {
				//更新项目负责人表员工编号
				bmsProjectLeaderMapper.updateLeaderJobNo(mdmUser.getJobNo(),resUser.getJobNo());
				//更新项目流程表
				//	更新审批人
				bmsWorkflowMapper.updateAuditorIdJobNo(mdmUser.getJobNo(),resUser.getJobNo());
				//  更新发起人
				bmsWorkflowMapper.updateOriginIdJobNo(mdmUser.getJobNo(),resUser.getJobNo());
				
				resUser.setPersonnelDept(mdmUser.getPersonnelDept());
				resUser.setJobNo(mdmUser.getJobNo());
				resUser.setNickName(mdmUser.getUserName());
				//更新用户表
				int res = sysUserMapper.updateUserByMdm(resUser);
				return res;
			}
		}else {
			return 0;
		}
		
		
	}

}
